package com.system.common.utils;

import java.io.*;
import java.util.concurrent.TimeUnit;

public class FileSearch {

    public static void main(String[] args) {
        boolean g = search('G');
        for (int i = 0; i < 100000; i++) {
            System.out.println(g);
        }
    }
    private static final String FILE_PATH = "D:\\test.txt";
    private static final int MAX_IO_COUNT_PER_MINUTE = 1000;

    public static boolean search(char c) {
        boolean result = false;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(FILE_PATH));
            String line;
            long lastIoTime = System.currentTimeMillis();
            int ioCount = 0;
            while ((line = reader.readLine()) != null) {
                if (line.contains("ABCDEF") && line.contains(String.valueOf(c))) {
                    result = true;
                    break;
                }
                if (++ioCount >= MAX_IO_COUNT_PER_MINUTE) {
                    long timePassed = System.currentTimeMillis() - lastIoTime;
                    if (timePassed < TimeUnit.MINUTES.toMillis(1)) {
                        Thread.sleep(TimeUnit.MINUTES.toMillis(1) - timePassed);
                    }
                    ioCount = 0;
                    lastIoTime = System.currentTimeMillis();
                }
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}
